package com.open.party.cloud.view.home.dangWuMiShu

import android.os.Bundle
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.ArticleEntity
import com.open.party.cloud.model.SuggestionVo
import com.open.party.cloud.viewModel.BizViewModel
import com.sinothk.android.utils.XUtils
import kotlinx.android.synthetic.main.activity_yi_jian_jian_yi.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class DwmsYiJIanJianYiActivity : AppEventBusTitleBaseActivity() {

    var viewModel: BizViewModel? = null

    override fun getLayoutResId(): Int = R.layout.activity_yi_jian_jian_yi

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("意见建议")

        viewModel = BizViewModel()

        submitBtn.setOnClickListener {
            val nameStr = userNameEt.text.toString()
            val phoneNumStr = phoneNumEt.text.toString()
            val contentStr = contentEt.text.toString()

            if (XUtils.string().isEmpty(nameStr)) {
                XUtils.toast().show("请输入姓名")
                return@setOnClickListener
            }

            if (XUtils.string().isEmpty(phoneNumStr)) {
                XUtils.toast().show("请输入联系方式")
                return@setOnClickListener
            }

            if (XUtils.string().isEmpty(contentStr)) {
                XUtils.toast().show("请输入意见建议内容")
                return@setOnClickListener
            }

            if (!XUtils.net().isAvailable) {
                XUtils.toast().show("当前网络不可用")
                return@setOnClickListener
            }

            val suggestion = SuggestionVo()
            suggestion.name = nameStr
            suggestion.contactInformation = phoneNumStr
            suggestion.suggestions = contentStr

            showLoadingDialog("提交中")
            viewModel!!.submitSuggestion(suggestion)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<ArticleEntity>>) {
        if (result.eventType != "submitSuggestion") {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    XUtils.toast().show("提交成功")
                    finish()
                } else {
                    XUtils.toast().show("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }
}